/*******************************
 *  КЛасс отвечающий за работу елемента screen
 *  работает с дом элементами - канвасами
 *
 ******************************/

var ttc_screen=function(conf){
	/************
	 *  VARS INIT
	 ***********/
  this.version='0.0.2';
	this.domObj=false;//объект
	this.canvas=[];   //Массив ссылок на дом объекты канвасов {dom:..., ctx:...} ключи - canvasID
	this.visible=[];   //Массив отображаемых
	
	this.errors_log=[];   //Буффер ошибок
	this.err=function(txt){
	  this.errors_log.push(txt);
	  return false;
	}
	this.errors=function(){
	  return this.errors_log.join(' ');
	}
	this.error=function(){
	  return this.errors_log[ this.errors_log.length-1 ];
	}

	this.init=function(){
	  var t=document.getElementById( this.domID );
	  if( typeof(t)=='undefined' || !t ){
			t = document.createElement( 'div' );
			if(!t) return false;
		  t.style.width = this.width		+'px';
		  t.style.height = this.height	+'px';
		  t.id = this.domID;
		  document.body.appendChild( t );
		}
		this.domObj=t;
		if(typeof(this.domObj)=='undefined' || !this.domObj) return this.err('DOM object for Screen undefined');
		return true;
	}
	
	/************
	 *  constructor - build and prepare self nesessaries
	 ***********/
	if(typeof(conf.parent)=='undefined'){
	  return this.err('conf.parent not passed');
	}
	this.editor=parent;

	if(typeof(conf.domID)=='undefined'){
	  return this.err('conf.domID not passed');
	}
	this.domID=conf.domID;

	if(typeof(conf.width)=='undefined'){
	  			this.width=800;
	}	else	this.width=width;

	if(typeof(conf.height)=='undefined'){
	  			this.height=600;
	}	else	this.height=height;
	if(!this.init()) return false;
	
	
	/***********************************
	 *  helper method to return obj variations
	 ************************************/
	this.returner=function(obj,mode){
	  if( typeof(obj)=='undefined' ) return this.err('returner:obj not passed');
	  if( typeof(mode)=='undefined' ) mode='ctx';
		switch(mode){
	    case 'obj':
	    case 'object':return obj;
	    case 'dom':return obj.dom;
	    case 'ctx': return obj.ctx;
	    default: return obj.id;
		}
	}
	/***********************************
	 *  return canvas context or dom (dep of <mode>) by <canvasId>
	 ************************************/
	this.get=function(canvasId,mode){
	  if( typeof(mode)=='undefined' ) mode='ctx';
	  if( typeof(canvasId)=='undefined' )	return this.err('get_canvas: canvasId not passed');
		if( typeof(this.canvas[ canvasId ])=='undefined' )	return this.err('get_canvas: canvas with id:'+canvasId+' not found');

		return this.returner(this.canvas[ canvasId ], mode);
	}
	
	/***********************************
	 *  creates new canvas object if not exist, returns it's ID
	 ************************************/
	this.create=function(canvasId,mode){
	  if(typeof(canvasId)=='undefined' || !canvasId ) canvasId=this.canvas.length;
		if( typeof(mode)=='undefined' ) mode='id';
		
	  var t=document.getElementById('canvas_'+canvasId);
		if( t )	return this.returner({
			  id:canvasId,
			  dom:t,
			  ctx:t.getContext( '2d' )
			},mode);
		
    var t = document.createElement( 'canvas' );
	  t.setAttribute( 'width',	this.width );
	  t.setAttribute( 'height',	this.height	);
	  t.id = 'canvas_'+canvasId;
	  t.style.display = 'none';
	  t.style.position = 'absolute';
	  t.style.top = 0;
	  t.style.left = 0;
	  this.domObj.appendChild(t);
	  this.canvas[canvasId]={
	    id:		canvasId,
	    dom:	t,
	    ctx:	t.getContext( '2d' )
		};
		return this.returner(this.canvas[canvasId],mode);
	}
	/***********************************
	 *  remove canvas DOM object and record from this.canvas index
	 ************************************/
	this.remove=function(canvasId){
	  var co=this.get(canvasId,'dom');
		if(!co) return false;
		co.remove();
		this.canvas[canvasId]=false;
		return true;
	}
	
	this.show=function(canvasId){
    var co=this.get(canvasId,'dom');
		if(!co) return false;
		co.style.display='inherit';
		return true;
	}
	this.hide=function(canvasId){
    var co=this.get(canvasId,'dom');
		if(!co) return false;
		co.style.display='none';
		return true;
	}
	this.hideAll=function(){
	  for( var i=0; i<this.canvas.length; i++ ){
	    if( !this.canvas[i] ) continue;
	    this.canvas[ i ].dom.style.display='none';
		}
		return true;
	}
	this.showOnly=function(canvasId){
	  for( var i=0; i<this.canvas.length; i++ ){
	    if( typeof(this.canvas[i])=='undefined' ) continue;
	    this.canvas[ i ].dom.style.display= (i==canvasId)?'inherit':'none';
		}
		return true;
	}
}